#include "bsp_aht21_driver.h"

static int8_t aht21_init(bsp_aht21_driver_t * aht21_instance)
{
    return 0;
}
static int8_t aht21_deinit(bsp_aht21_driver_t * aht21_instance)
{
    return 0;
}
static int8_t aht21_read_id (bsp_aht21_driver_t * aht21_instance)
{
    
    return 0;
}
static int8_t aht21_read_temperature(bsp_aht21_driver_t* aht21_instance, 
                                    float* ptemperature)
{
    return 0;
}
static int8_t aht21_read_humidity(bsp_aht21_driver_t* aht21_instance,
                                 float* phumidity)
{
    return 0;
}

static int8_t aht21_deinst(bsp_aht21_driver_t * aht21_instance)
{
    if (aht21_instance != NULL)
    {   
       //iic driver deinit 
        if (aht21_instance->psiic_driver_interface !=NULL)
        {
            aht21_instance->psiic_driver_interface->pfinit           = NULL;
            aht21_instance->psiic_driver_interface->pfdeinit         = NULL;
            aht21_instance->psiic_driver_interface->pfstart          = NULL;
            aht21_instance->psiic_driver_interface->pfstop           = NULL;
            aht21_instance->psiic_driver_interface->pfwait_ack       = NULL;
            aht21_instance->psiic_driver_interface->pfsend_ack       = NULL;
            aht21_instance->psiic_driver_interface->pfread_bytes     = NULL;
            aht21_instance->psiic_driver_interface->pfwrite_bytes    = NULL;
        }

        //timebase driver deinit
        if (aht21_instance->pstimebase_interface != NULL)
        {
            aht21_instance->pstimebase_interface->pfget_tick_count   = NULL;
        }

        //aht21 deinit
        aht21_instance->pfdeinit            = NULL;
        aht21_instance->pfread_id           = NULL;
        aht21_instance->pfread_humidity     = NULL;
        aht21_instance->pfread_temperature  = NULL;
    }
}

int8_t aht21_inst(bsp_aht21_driver_t* aht21_instance,
                 iic_driver_interface_t* iic_instance,
                 timebase_interface_t* timebase_instance
                 )
{
    /* 1.instance not null */
    if (NULL == aht21_instance)
    {
        return -1;
    }

    if (NULL == iic_instance)
    {
        return -2;
    }

    if (NULL == timebase_instance)
    {
        return -3;
    }

    /*2. load iic driver */
    aht21_instance->psiic_driver_interface     = iic_instance;
    if (NULL == iic_instance->pfinit         ||
        NULL == iic_instance->pfdeinit       ||
        NULL == iic_instance->pfstart        ||
        NULL == iic_instance->pfstop         ||
        NULL == iic_instance->pfwait_ack     ||
        NULL == iic_instance->pfsend_ack     ||
        NULL == iic_instance->pfwrite_bytes  ||
        NULL == iic_instance->pfread_bytes 
        )
    {
        /* deint  iic driver */
        aht21_instance->psiic_driver_interface->pfdeinit();
        return -4;
    }

    /* 3.load timetbase */
    aht21_instance->pstimebase_interface       = timebase_instance;
    if (timebase_instance->pfget_tick_count == NULL)
    {
        /*deinit  timetbase */
        return -5;
    } 

    /* 4.aht21 initialization */
    aht21_instance->pfinit                     = aht21_init;
    aht21_instance->pfdeinit                   = aht21_deinit;
    aht21_instance->pfdeinst                   = aht21_deinst;
    aht21_instance->pfread_id                  = aht21_read_id;
    aht21_instance->pfread_temperature         = aht21_read_temperature;
    aht21_instance->pfread_humidity            = aht21_read_humidity;

    return 0;
}                            

